﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using F = System.Windows.Forms;
using V = Microsoft.VisualStudio;

namespace Packet_AC_A
{
    [Guid(GuidList.guidPacket_AC_APkgString)]
    public class Packet_AC_APackage : Packet_AC_APackageBase
    {

        /// <summary>
        ///
        /// </summary>
        /// <param name="ServiceType"></param>
        /// <returns>Code: <code>return base.GetService(ServiceType);</code></returns>
        public object GetServicePublic(Type ServiceType)
        {
            return base.GetService(ServiceType);
        }

        /// <summary>
        ///Returns the Extensions DTE2 in A VB.Net + C# Friendly way.
        ///
        /// Currently This only Works for C# *Cries* [You can use it in VB.NET just Not Expand it]
        /// </summary>
        /// <param name="ServiceType"></param>
        /// <returns>Code: <code>return (EnvDTE80.DTE2)base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));</code></returns>
        public EnvDTE.DTE GetDTE2()
        {
            //This Seemed to fix the Problem of Expanding the COM Object...O.o

            //var Test = base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));

            //		(EnvDTE80.DTE2)Test	Embedded interop type 'EnvDTE80.DTE2' is defined in both 'EnvDTE80.dll' and 'BaseProject.dll'. Some operations on objects of this type are not supported while debugging. Consider casting this object to type 'dynamic' when debugging or building with the 'Embed Interop Types' property set to false.
            //		(EnvDTE80.DTE2)Test	Embedded interop type 'EnvDTE80.DTE2' is defined in both 'EnvDTE80.dll' and 'ALinq.ORDesignerLoader.dll'. Some operations on objects of this type are not supported while debugging. Consider casting this object to type 'dynamic' when debugging or building with the 'Embed Interop Types' property set to false.
            //		(EnvDTE80.DTE2)Test	Embedded interop type 'EnvDTE80.DTE2' is defined in both 'EnvDTE80.dll' and 'ALinq.ORDesignerLoader.dll'. Some operations on objects of this type are not supported while debugging. Consider casting this object to type 'dynamic' when debugging or building with the 'Embed Interop Types' property set to false.

            //		(EnvDTE.DTE)base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));	Embedded interop type 'EnvDTE.DTE' is defined in both 'VSPackage1.dll' and 'EnvDTE.dll'. Some operations on objects of this type are not supported while debugging. Consider casting this object to type 'dynamic' when debugging or building with the 'Embed Interop Types' property set to false.
            //		(EnvDTE.DTE)base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));	Embedded interop type 'EnvDTE.DTE' is defined in both 'EnvDTE.dll' and 'NuGet.Tools.dll'. Some operations on objects of this type are not supported while debugging. Consider casting this object to type 'dynamic' when debugging or building with the 'Embed Interop Types' property set to false.

            return (EnvDTE.DTE)base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
        }





        protected override void Initialize()
        {
            base.Initialize();
            //PHelp.Mod_Utilities.Copy_DLLs();
        }






        protected override void Button2ExecuteHandler(object sender, EventArgs e)
        {
            EnvDTE.DTE app = (EnvDTE.DTE)GetService(typeof(SDTE));
            if (app.ActiveDocument != null && app.ActiveDocument.Type == "Text")
            {
                EnvDTE.TextDocument text = (EnvDTE.TextDocument)app.ActiveDocument.Object(String.Empty);
                if (!text.Selection.IsEmpty)
                {
                    if (F.MessageBox.Show("Would you like to Add A New to the Class", "Add New", F.MessageBoxButtons.YesNo) == F.DialogResult.Yes)
                    {
                        string New_Text = text.Selection.Text.Split(new string[] { "\r" }, StringSplitOptions.None).First();
                        New_Text = New_Text + "\r\nSub New()\nEnd Sub";

                        List<string> List = frm_AddNew.Grab();

                        foreach (string item in List)
                        {
                            New_Text = New_Text + "\r" + item;
                        }

                        New_Text = New_Text + "\r" + text.Selection.Text.Split(new string[] { "\r" }, StringSplitOptions.None).Last();

                        text.Selection.Text = New_Text;
                        text.Selection.SelectAll();
                        text.Selection.SmartFormat();

                        //text.ReplacePattern("", "A");   //EnvDTE.vsFindOptions.vsFindOptionsNone
                    }

                    //work with text.Selection.Text
                }
            }
        }

        protected override void HomePageExecuteHandler(object sender, EventArgs e)
        {
            PHelp.frmShortcuts _Form = new PHelp.frmShortcuts();
            _Form.ShowDialog();
        }

        //protected override void Button4ExecuteHandler(object sender, EventArgs e)
        //{
        //    EnvDTE.DTE app = (EnvDTE.DTE)GetService(typeof(SDTE));
        //    //app.Windows.Item(2).
        //}

        protected override void Remove_Bad_WindowsExecuteHandler(object sender, EventArgs e)
        {
            EnvDTE.DTE app = (EnvDTE.DTE)GetService(typeof(SDTE));
            List<EnvDTE.Window> List = new List<EnvDTE.Window>();

            foreach (EnvDTE.Window item in app.Windows)
            {
                switch (item.Caption)
                {
                    case "Data Sources":
                    case "Solution Explorer":
                    case "Team Explorer":
                    case "Server Explorer":
                        List.Add(item);
                        break;
                    case "Properties":
                    case "Toolbox":
                        item.AutoHides = true;
                        break;
                    default:
                        if (item.Caption.Contains("Solution Explorer"))
                        {
                            List.Add(item);
                        }
                        break;
                }
            }

            for (int i = 0; i < List.Count; i++)
            {
                List[i].Close();
            }
        }

        protected override void Add_Good_WindowsExecuteHandler(object sender, EventArgs e)
        {
            EnvDTE.DTE app = (EnvDTE.DTE)GetService(typeof(SDTE));
            List<string> Test = new List<string>();
            foreach (EnvDTE.Window subitem in app.Windows)
            {
                Test.Add(subitem.Caption);
            }

            V.CommandBars.CommandBars CommandBars = app.CommandBars as V.CommandBars.CommandBars;

            CommandBars.Add("Test1");

            V.CommandBars.CommandBar New_Bar;

            List<string> CommandBarList = new List<string>();
            foreach (V.CommandBars.CommandBar item in CommandBars)
            {
                CommandBarList.Add(item.Name);

                if (item.Name == "Test1")
                {
                    New_Bar = item;
                }
            }

            System.Drawing.Design.IToolboxService tbxService = (System.Drawing.Design.IToolboxService)GetService(typeof(System.Drawing.Design.IToolboxService));

            System.Drawing.Design.ToolboxItem New_ToolItem = new System.Drawing.Design.ToolboxItem();
            New_ToolItem.TypeName = "System.Windows.Forms.Button";
            New_ToolItem.Description = "Hello";
            New_ToolItem.DisplayName = "Test_Button";

            tbxService.AddToolboxItem(New_ToolItem);

            //var aa = new DevProjects.AutoCode.UI.CatalogControl();
        }

        protected override void btnTest1ExecuteHandler(object sender, EventArgs e)
        {
            System.Drawing.Design.IToolboxService tbxService = (System.Drawing.Design.IToolboxService)GetService(typeof(System.Drawing.Design.IToolboxService));

            PHelp.Mod_ToolBox.Clean_ToolBox(tbxService);
        }

        protected override void btnSnippetsExecuteHandler(object sender, EventArgs e)
        {
            PHelp.frmSnippets _Form = new PHelp.frmSnippets();
            _Form.Show();
        }

        protected override void Button3ExecuteHandler(object sender, EventArgs e)
        {
            List<Int32> AAT = new List<Int32>();
            AAT.Add(1);
            AAT.Add(2);
            AAT.Add(3);

            var iSmall =
                from it in AAT
                where it > 1
                select it;
        }
    }
}